package games.rolling;

import gps_api.State;

public class RollingState implements State{
	static final int SIZE_TABLE=3;
	protected int [][]table;
	protected int emptyPos;
	
	public RollingState(){
		table= new int[SIZE_TABLE][SIZE_TABLE];
	}
	
	public boolean equals(Object arg0){
		boolean ans=true;
		RollingState state;
		
		if(arg0 instanceof RollingState){
			state= (RollingState)arg0;
			
			for(int x=0; x<SIZE_TABLE && ans; x++){
				for(int y=0; y<SIZE_TABLE && ans; y++){
					if(table[x][y]!=state.table[x][y]){
						ans= false;	
					}
				}
			}
			ans=isSimetric(state);
			
		}else{
			ans= false;
		}
			
		
		return ans;
		
	}
	
	public boolean compare(RollingState state){
		boolean ans=true;
		
		for(int x=0; x<SIZE_TABLE && ans; x++){
			for(int y=0; y<SIZE_TABLE && ans; y++){
				if(table[x][y]!=state.table[x][y]){
					ans= false;	
				}
			}
		}
		
		return ans;
	}
	
	
	public String toString(){
		String s = "";
		for(int x=0; x<SIZE_TABLE; x++){
			for(int y=0; y<SIZE_TABLE; y++){
				switch(table[x][y]){
				case RollingToken.WHITE: s+="WHITE |"; break;
				case RollingToken.BLACK: s+="BLACK |"; break;
				case RollingToken.EMPTY: s+="EMPTY |"; break;
				case RollingToken.H_BW: s+="H_BW |"; break;
				case RollingToken.H_WB: s+="H_WB |"; break;
				case RollingToken.V_BW: s+="V_BW |"; break;
				case RollingToken.V_WB: s+="V_WB |"; break;
				}
			}
			s+="\n";
		}
		return s;
	}

	public void setEmptyPos(int y, int x){
		emptyPos= y*SIZE_TABLE + x;
	}
	
	public boolean isSimetric(RollingState arg) {
		boolean ans= false;
		RollingState state= arg;
		
		for(int i=0; i<4 & !ans; i++ ){
			ans=compare(state);
			state= state.rotate90(state);
		}
		
		return ans;
	}
	
	public RollingState rotate90(RollingState state){
		RollingState newState= new RollingState();
		
		newState.table[0][0]= RollingToken.rotate90[state.table[2][0]];
		newState.table[0][1]= RollingToken.rotate90[state.table[1][0]];
		newState.table[0][2]= RollingToken.rotate90[state.table[0][0]];
		
		newState.table[1][0]= RollingToken.rotate90[state.table[2][1]];
		newState.table[1][1]= RollingToken.rotate90[state.table[1][1]];
		newState.table[1][2]= RollingToken.rotate90[state.table[0][1]];
		
		newState.table[2][0]= RollingToken.rotate90[state.table[2][2]];
		newState.table[2][1]= RollingToken.rotate90[state.table[1][2]];
		newState.table[2][2]= RollingToken.rotate90[state.table[0][2]];
		
		return newState;
	}
	

}
